# 完整的前端依赖修复脚本
Write-Host "=== 前端项目依赖完整修复脚本 ===" -ForegroundColor Cyan

# 检查 Node.js 和 npm 版本
Write-Host "`n1. 检查环境信息" -ForegroundColor Yellow
Write-Host "Node.js 版本: $(node --version)" -ForegroundColor White
Write-Host "npm 版本: $(npm --version)" -ForegroundColor White

# 检查当前目录
Write-Host "`n2. 检查当前目录" -ForegroundColor Yellow
Write-Host "当前目录: $(Get-Location)" -ForegroundColor White
if (Test-Path "package.json") {
    Write-Host "✓ 找到 package.json" -ForegroundColor Green
} else {
    Write-Host "✗ 未找到 package.json，请确保在正确的目录中运行此脚本" -ForegroundColor Red
    exit 1
}

# 停止可能运行的进程
Write-Host "`n3. 停止可能运行的开发服务器" -ForegroundColor Yellow
Get-Process | Where-Object { $_.ProcessName -eq "node" } | Stop-Process -Force -ErrorAction SilentlyContinue
Write-Host "✓ 已停止相关进程" -ForegroundColor Green

# 清理现有文件
Write-Host "`n4. 清理现有文件" -ForegroundColor Yellow
if (Test-Path "node_modules") {
    Remove-Item -Recurse -Force "node_modules"
    Write-Host "✓ 已删除 node_modules" -ForegroundColor Green
}
if (Test-Path "package-lock.json") {
    Remove-Item -Force "package-lock.json"
    Write-Host "✓ 已删除 package-lock.json" -ForegroundColor Green
}

# 清理缓存
Write-Host "`n5. 清理缓存" -ForegroundColor Yellow
npm cache clean --force
Write-Host "✓ npm 缓存已清理" -ForegroundColor Green

# 更新 npm 到最新版本
Write-Host "`n6. 更新 npm" -ForegroundColor Yellow
npm install -g npm@latest
Write-Host "✓ npm 已更新" -ForegroundColor Green

# 重新安装依赖
Write-Host "`n7. 重新安装依赖" -ForegroundColor Yellow
Write-Host "正在安装依赖，这可能需要几分钟..." -ForegroundColor White
npm install --verbose

# 验证关键依赖
Write-Host "`n8. 验证关键依赖" -ForegroundColor Yellow
$criticalDeps = @("axios", "vue", "vue-router", "pinia", "element-plus")
foreach ($dep in $criticalDeps) {
    if (Test-Path "node_modules\$dep") {
        Write-Host "✓ $dep 安装成功" -ForegroundColor Green
    } else {
        Write-Host "✗ $dep 安装失败" -ForegroundColor Red
    }
}

# 检查 axios 具体版本
if (Test-Path "node_modules\axios\package.json") {
    $axiosVersion = (Get-Content "node_modules\axios\package.json" | ConvertFrom-Json).version
    Write-Host "✓ axios 版本: $axiosVersion" -ForegroundColor Green
}

Write-Host "`n=== 修复完成 ===" -ForegroundColor Cyan
Write-Host "现在可以运行以下命令启动项目:" -ForegroundColor White
Write-Host "npm run serve" -ForegroundColor Yellow
Write-Host "`n如果仍有问题，请检查:" -ForegroundColor White
Write-Host "1. Node.js 版本是否 >= 14.0.0" -ForegroundColor White
Write-Host "2. 网络连接是否正常" -ForegroundColor White
Write-Host "3. 是否有防火墙阻止 npm" -ForegroundColor White
